package com.gxa.saas.controller;

import com.gxa.saas.pojo.Purchase;
import com.gxa.saas.pojo.PurchaseRequest;
import com.gxa.saas.pojo.Storage;
import com.gxa.saas.service.PurchaseRequestService;
import com.gxa.saas.service.StorageService;
import com.gxa.saas.utils.JsonResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/PurchaseRequest")
public class PurchaseRequestController {

    @Autowired
    PurchaseRequestService purchaseRequestService;


    @ApiOperation("添加操作")
    @RequestMapping(value = "/insert",method = {RequestMethod.POST})
    public Map insert(Integer id,Integer number){
        Storage storage = purchaseRequestService.selectById(id);
        List<PurchaseRequest> purchaseRequests = purchaseRequestService.selectByNumber(id);
        Integer size = purchaseRequests.size();

        PurchaseRequest purchaseRequest = new PurchaseRequest();

        StringBuilder big = new StringBuilder();
        StringBuilder small = new StringBuilder();

        String bigUnit = storage.getBigUnit();
        String smallUnit = storage.getSmallUnit();

        Double bigPrice = storage.getBigPrice();
        Double smallPrice = storage.getSmallPrice();

        String bigPrice1 = String.valueOf(storage.getBigPrice());
        String smallPrice1 = String.valueOf(storage.getSmallPrice());

        purchaseRequest.setPId(number);

        big.append(bigPrice1);
        big.append("/");
        big.append(bigUnit);
        purchaseRequest.setBigUnitPrice(big.toString());

        small.append(smallPrice1);
        small.append("/");
        small.append(smallUnit);
        purchaseRequest.setSmallUnitPrice(small.toString());

        purchaseRequest.setName(storage.getName());
        purchaseRequest.setId(storage.getId());
        purchaseRequest.setPId(id);
        purchaseRequest.setBigAmount(1);
        purchaseRequest.setSmallAmount(1);
        purchaseRequest.setMoney(bigPrice*1+smallPrice*1);

//        bigPrice*1+smallPrice*1
        JsonResult jsonResult = new JsonResult();
        Integer insert = purchaseRequestService.insert(purchaseRequest);

        jsonResult.setCode("200");
        jsonResult.setMsg("添加成功");
        jsonResult.setData(insert);
        jsonResult.set("purchaseRequest",purchaseRequest);

        Double money = 0.00;
        for (int i = 0; i < size; i++) {
            Double money1 = purchaseRequests.get(i).getMoney();
            money = money+money1;
        }
        jsonResult.set("money",money);


        Integer sum = 0;
        for (int i = 1; i < size; i++) {
            Integer bigAmount = purchaseRequests.get(i).getBigAmount();
            sum = bigAmount+sum;
        }
        jsonResult.set("sum",sum);
        return jsonResult.getMap();
    }

    @ApiOperation("第二页分页查询")
    @RequestMapping(value = "/selectBySecondPage",method = {RequestMethod.POST})
    public Map selectByPage(@RequestParam(value = "page") Integer offset,
                            @RequestParam(value = "size",required = false,defaultValue = "10")Integer size){
        List<PurchaseRequest> purchaseRequests = purchaseRequestService.selectBySecondPage(offset, size);
        Integer gotal = purchaseRequestService.getGotal();
        JsonResult jsonResult = new JsonResult();
        jsonResult.set("gotal",gotal);
        jsonResult.setCode("200");
        jsonResult.setData(purchaseRequests);
        return jsonResult.getMap();
    }

    @ApiOperation("根据商品名称模糊分页查询")
    @RequestMapping(value = "/selectLikeName",method = {RequestMethod.POST})
    public Map selectLikeName(@RequestParam(value = "name") String name,
                              @RequestParam(value = "page") Integer offset,
                              @RequestParam(value = "size",required = false,defaultValue = "10") Integer size){
        List<PurchaseRequest> purchaseRequests = purchaseRequestService.selectLikeName(name, size * (offset - 1), size);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(purchaseRequests);
        return jsonResult.getMap();
    }

    @ApiOperation("修改")
    @RequestMapping(value = "/update",method = {RequestMethod.POST})
    @ResponseBody
    public Map update(@RequestParam() Purchase purchase){
        JsonResult jsonResult = new JsonResult();
        Integer update = purchaseRequestService.update(purchase);
        jsonResult.setCode("200");
        jsonResult.setData(update);
        return jsonResult.getMap();
    }

    @ApiOperation("删除")
    @RequestMapping(value = "/delete",method = {RequestMethod.POST})
    public Map delete(@RequestParam Integer id){
        JsonResult jsonResult = new JsonResult();
        Integer delete = purchaseRequestService.delete(id);
        jsonResult.setCode("200");
        jsonResult.setMsg("删除成功");
        jsonResult.setData(delete);
        return jsonResult.getMap();
    }

    @ApiOperation("供应商下拉框")
    @RequestMapping(value = "/selectBySupplier",method = {RequestMethod.POST})
    public Map selectBySupplier(){
        JsonResult jsonResult = new JsonResult();
        List<PurchaseRequest> purchaseRequests = purchaseRequestService.selectBySupplier();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(purchaseRequests);
        return jsonResult.getMap();
    }

    @ApiOperation("付款方式下拉框")
    @RequestMapping(value = "/selectByPayType",method = {RequestMethod.POST})
    public Map selectByPayType(){
        JsonResult jsonResult = new JsonResult();
        List<PurchaseRequest> purchaseRequests = purchaseRequestService.selectByPayType();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(purchaseRequests);
        return jsonResult.getMap();
    }

    @ApiOperation("审批人下拉框")
    @RequestMapping(value = "/selectByApprover",method = {RequestMethod.POST})
    public Map selectByApprover(){
        JsonResult jsonResult = new JsonResult();
        List<PurchaseRequest> purchaseRequests = purchaseRequestService.selectByApprover();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(purchaseRequests);
        return jsonResult.getMap();
    }

    @ApiOperation("创建人下拉框")
    @RequestMapping(value = "/selectByCreatePerson",method = {RequestMethod.POST})
    public Map selectByCreatePerson(){
        JsonResult jsonResult = new JsonResult();
        List<PurchaseRequest> purchaseRequests = purchaseRequestService.selectByCreatePerson();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(purchaseRequests);
        return jsonResult.getMap();
    }

    @ApiOperation("提交到第一页")
    @RequestMapping(value = "/submit",method = {RequestMethod.POST})
    @ResponseBody
    public Map submit(@RequestBody Purchase purchase){
        JsonResult jsonResult = new JsonResult();
        Integer submit = purchaseRequestService.submit(purchase);
        Integer total = purchaseRequestService.getGotal();
        jsonResult.setCode("200");
        jsonResult.setData(submit);
        jsonResult.set("total",total);
        return jsonResult.getMap();
    }

    @ApiOperation("根据订单编号number查询")
    @RequestMapping(value = "/selectByNumber",method = {RequestMethod.POST})
    public Map selectByNumber(Integer id){
        JsonResult jsonResult = new JsonResult();
        List<PurchaseRequest> purchaseRequests = purchaseRequestService.selectByNumber(id);
        jsonResult.setCode("200");
        jsonResult.setData(purchaseRequests);
        return jsonResult.getMap();
    }


    @ApiOperation("加减接口统一调用")
    @RequestMapping(value = "/updateByAmount",method = {RequestMethod.POST})
    public Map updateByAmount(Integer pid,Integer id,Integer bigAmount,Integer smallAmount){

        PurchaseRequest purchaseRequest = purchaseRequestService.selectByPrId(id);
        System.out.println("Prid:"+id);

        Storage storage = purchaseRequestService.selectById(id);
        System.out.println("id:"+id);

        List<PurchaseRequest> purchaseRequests = purchaseRequestService.selectByNumber(pid);
        System.out.println("pid:"+pid);

        Integer size = purchaseRequests.size();

        String bigUnitPrice1 = purchaseRequest.getBigUnitPrice();
        int i = bigUnitPrice1.indexOf("/");
        String bigPrice = bigUnitPrice1.substring(0,i);

        String smallUnitPrice1 = purchaseRequest.getSmallUnitPrice();
        int j = smallUnitPrice1.indexOf("/");
        String smallPrice = smallUnitPrice1.substring(0, j);

        Double bigUnitPrice = Double.valueOf(bigPrice);
        Double smallUnitPrice = Double.valueOf(smallPrice);

        Double money = bigUnitPrice*bigAmount+smallUnitPrice*smallAmount;
        purchaseRequestService.updateByAmount(id, bigAmount, smallAmount, money);


//        System.out.println("storage.getBigPrice():"+storage.getBigPrice());
//        Double bigPrice1 = storage.getBigPrice();
//        Double smallPrice1 = storage.getSmallPrice();

        purchaseRequest.setBigAmount(1);
        purchaseRequest.setSmallAmount(1);
        purchaseRequest.setMoney(bigUnitPrice*1+smallUnitPrice*1);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        Double money2 = 0.00;
        for (int k = 0; k < size; k++) {
            Double money1 = purchaseRequests.get(k).getMoney();
            money2 = money+money1;
        }
        jsonResult.set("money",money2);

        Integer sum = 0;
        for (int l = 1; l < size; l++) {
            Integer bigAmount1 = purchaseRequests.get(l).getBigAmount();
            sum = bigAmount1+sum;
        }
        jsonResult.set("sum",sum);
        return jsonResult.getMap();
    }

    @ApiOperation("第二页的第二个表全查询")
    @RequestMapping(value = "/selectAll",method = {RequestMethod.POST})
    public Map selectAll(){
        JsonResult jsonResult = new JsonResult();
        List<PurchaseRequest> purchaseRequests = purchaseRequestService.selectAll();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(purchaseRequests);
        return jsonResult.getMap();
    }

    @ApiOperation("第二页的根据id查询，预览详情用")
    @RequestMapping(value = "/selectByFid",method = {RequestMethod.POST})
    public Map selectByFid(Integer id){
        List<PurchaseRequest> purchaseRequests = purchaseRequestService.selectByFid(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(purchaseRequests);
        return jsonResult.getMap();
    }

    @ApiOperation("编辑")
    @RequestMapping(value = "/selectTwoList",method = {RequestMethod.POST})
    public Map selectTwoList(Integer id){
        List<PurchaseRequest> purchaseRequests = purchaseRequestService.selectByFid(id);
        List<Purchase> purchases = purchaseRequestService.selectByUid(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.set("purchaseRequests",purchaseRequests);
        jsonResult.set("purchases",purchases);
        return jsonResult.getMap();
    }


}
